{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "NKas2cYws8F6"
   },
   "source": [
    "# Allegro ClearML matplotlib example\n",
    "\n",
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/allegroai/clearml/blob/master/examples/frameworks/matplotlib/jupyter_matplotlib_example.ipynb)\n",
    "\n",
    "This example introduces ClearML with matplotlib functionality. It also shows seaborn functionality. You can find more frameworks examples [here](https://github.com/allegroai/clearml/tree/master/examples/frameworks).\n",
    "\n",
    "Note: This example is based on the ClearML [matplotlib_example.py](https://github.com/allegroai/clearml/blob/master/examples/frameworks/matplotlib/matplotlib_example.py) example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "72lCj7MJmRkQ"
   },
   "outputs": [],
   "source": [
    "# If you don't have ClearML installed then uncomment this line\n",
    "# !pip install clearml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install seaborn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "b8jtq0iSt3-U"
   },
   "source": [
    "## Create a new task.\n",
    "\n",
    "To create a new Task object, call the `Task.init` method providing it with `project_name` (the project name for the experiment) and `task_name` (the name of the experiment). When `Task.init` executes, a link to the Web UI Results page for the newly generated Task will be printed, and the Task will be updated in real time in the ClearML demo server.\n",
    "\n",
    "You can read about the `Task` class in the docs [here](https://clear.ml/docs/latest/docs/fundamentals/task)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ses67ulJkGPq"
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "\n",
    "from clearml import Task\n",
    "\n",
    "# Start a new task\n",
    "task = Task.init(project_name=\"Colab notebooks\", task_name=\"Matplotlib example\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "T2l-0WvJt_yo"
   },
   "source": [
    "## Matplotlib support\n",
    "\n",
    "ClearML automatically logs Matplotlib plots. They appear in the Web UI Results tab.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "a-nOyg9xlxiR"
   },
   "outputs": [],
   "source": [
    "# create plot\n",
    "N = 50\n",
    "x = np.random.rand(N)\n",
    "y = np.random.rand(N)\n",
    "colors = np.random.rand(N)\n",
    "area = (30 * np.random.rand(N))**2  # 0 to 15 point radii\n",
    "plt.scatter(x, y, s=area, c=colors, alpha=0.5)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "iV4BtqRFmi0N"
   },
   "outputs": [],
   "source": [
    "# create another plot - with a name\n",
    "x = np.linspace(0, 10, 30)\n",
    "y = np.sin(x)\n",
    "plt.plot(x, y, 'o', color='pink')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "yKT5UjDk6DGB"
   },
   "source": [
    "By calling the `imshow` method, ClearML automatically reports plot images in Results tab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "IVzUScalmio-"
   },
   "outputs": [],
   "source": [
    "# create unitlted image plot\n",
    "m = np.eye(256, 256, dtype=np.uint8)\n",
    "plt.imshow(m)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "mioKlXpimib1"
   },
   "outputs": [],
   "source": [
    "# create image plot - with a name\n",
    "m = np.eye(256, 256, dtype=np.uint8)\n",
    "plt.imshow(m)\n",
    "plt.title('Image Title')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "AE7Gbm3GfvvK"
   },
   "outputs": [],
   "source": [
    "# create plot with savefig\n",
    "N = 10\n",
    "x = np.random.rand(N)\n",
    "y = np.random.rand(N)\n",
    "colors = np.random.rand(N)\n",
    "area = (30 * np.random.rand(N))**2\n",
    "plt.title('savefig Image')\n",
    "plt.scatter(x, y, s=area, c=colors, alpha=0.5)\n",
    "plt.savefig(\"plot.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jcRWq9Xc56fX"
   },
   "source": [
    "## Seaborn support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "j-usk2d_mqS4"
   },
   "outputs": [],
   "source": [
    "sns.set(style=\"darkgrid\")\n",
    "# Load an example dataset with long-form data\n",
    "fmri = sns.load_dataset(\"fmri\")\n",
    "# Plot the responses for different events and regions\n",
    "sns.lineplot(x=\"timepoint\", y=\"signal\",\n",
    "             hue=\"region\", style=\"event\",\n",
    "             data=fmri)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "Allegro ClearML matplotlib example.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
